#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System.Linq;
using WaveTech.GShield.Model;

#endregion

namespace WaveTech.GShield.Data
{
	public static class RatingsDAO
	{
		#region Get Rating

		public static Rating GetRating(string title, string fileName, string productName)
		{
			var rating = from r in Database.DC.Ratings
			             where r.InternalName == title && r.FileName == fileName && r.ProductName == productName
			             select r;

			return rating.Single();
		}

		#endregion Get Rating

		public static void AddNewRating(GameRatingDetail rating, string title, string publisher, string fileName,
		                                string directory, string productName)
		{
			Rating r = new Rating();
			r.GameName = rating.Name;
			r.Publisher = rating.Publisher;
			r.GameContent = rating.Content;
			r.EsrbRating = rating.Rating;
			r.RatingType = ((int) rating.GetRatingType()).ToString();
			r.Platforms = rating.Platforms;
			r.FileName = fileName;
			r.InternalName = title;
			r.ProductName = productName;
			r.Publisher = publisher;
			r.DirectoryName = directory;

			Database.DC.Ratings.InsertOnSubmit(r);
			Database.DC.SubmitChanges();
		}
	}

	#region Old RatingsDAO

	/*
  /// <summary>
  /// Ratings Data Access Objects
  /// </summary>
  public class RatingsDAO
  {
    #region Add New Rating
    public void AddNewRating(GameRatingDetail rating, string title, string publisher, string fileName, string directory, string productName)
    {
      Database db = DatabaseFactory.CreateDatabase();
      DbCommand dbCommand = db.GetStoredProcCommand("AddNewRating");

      db.AddInParameter(dbCommand, "@GameName", DbType.String, rating.Name);
      db.AddInParameter(dbCommand, "@Publisher", DbType.String, rating.Publisher);
      db.AddInParameter(dbCommand, "@Content", DbType.String, rating.Content);
      db.AddInParameter(dbCommand, "@Rating", DbType.String, rating.Rating);
      db.AddInParameter(dbCommand, "@RatingTypes", DbType.Int32, (int)rating.GetRatingType());
      db.AddInParameter(dbCommand, "@Platforms", DbType.String, rating.Platforms);
      db.AddInParameter(dbCommand, "@FileName", DbType.String, fileName);
      db.AddInParameter(dbCommand, "@InternalName", DbType.String, title);
      db.AddInParameter(dbCommand, "@ProductName", DbType.String, productName);
      db.AddInParameter(dbCommand, "@Company", DbType.String, publisher);
      db.AddInParameter(dbCommand, "@DirectoryName", DbType.String, directory);

      db.ExecuteNonQuery(dbCommand);
    }
    #endregion Add new Rating

    #region Get Rating
    public Rating GetRating(string title, string fileName, string productName)
    {
      Database db = DatabaseFactory.CreateDatabase();
      DbCommand dbCommand = db.GetStoredProcCommand("GetRatingType");

      db.AddInParameter(dbCommand, "@FileName", DbType.String, fileName);
      db.AddInParameter(dbCommand, "@Title", DbType.String, title);
      db.AddInParameter(dbCommand, "@ProductName", DbType.String, productName);

      return FillRating(db.ExecuteReader(dbCommand));
    }
    #endregion Get Rating

    #region Fill Rating
    private Rating FillRating(IDataReader reader)
    {
      if (reader == null)
        return null;

      Rating rate = null;

      if (reader.Read())
      {
        rate = new Rating();

        if (!reader.IsDBNull(reader.GetOrdinal("RatingId")))
          rate.RatingId = reader.GetInt64(reader.GetOrdinal("RatingId"));

        if (!reader.IsDBNull(reader.GetOrdinal("GameName")))
          rate.GameName = reader.GetString(reader.GetOrdinal("GameName"));

        if (!reader.IsDBNull(reader.GetOrdinal("Publisher")))
          rate.Publisher = reader.GetString(reader.GetOrdinal("Publisher"));

        if (!reader.IsDBNull(reader.GetOrdinal("Content")))
          rate.Content = reader.GetString(reader.GetOrdinal("Content"));

        if (!reader.IsDBNull(reader.GetOrdinal("Rating")))
          rate.EsrbRating = reader.GetString(reader.GetOrdinal("Rating"));

        if (!reader.IsDBNull(reader.GetOrdinal("RatingTypes")))
          rate.RatingTypes = (RatingTypes)reader.GetInt32(reader.GetOrdinal("RatingTypes"));

        if (!reader.IsDBNull(reader.GetOrdinal("Platforms")))
          rate.EsrbRating = reader.GetString(reader.GetOrdinal("Platforms"));

        if (!reader.IsDBNull(reader.GetOrdinal("FileName")))
          rate.FileName = reader.GetString(reader.GetOrdinal("FileName"));

        if (!reader.IsDBNull(reader.GetOrdinal("InternalName")))
          rate.InternalName = reader.GetString(reader.GetOrdinal("InternalName"));

        if (!reader.IsDBNull(reader.GetOrdinal("Company")))
          rate.Company = reader.GetString(reader.GetOrdinal("Company"));

        if (!reader.IsDBNull(reader.GetOrdinal("DirectoryName")))
          rate.DirectoryName = reader.GetString(reader.GetOrdinal("DirectoryName"));

        if (!reader.IsDBNull(reader.GetOrdinal("DateAdded")))
          rate.DateAdded = reader.GetDateTime(reader.GetOrdinal("DateAdded"));

        if (!reader.IsDBNull(reader.GetOrdinal("Verified")))
          rate.Verified = reader.GetBoolean(reader.GetOrdinal("Verified"));

        if (!reader.IsDBNull(reader.GetOrdinal("DateVerified")))
          rate.DateVerified = reader.GetDateTime(reader.GetOrdinal("DateVerified"));

        if (!reader.IsDBNull(reader.GetOrdinal("VerifiedBy")))
          rate.VerifiedBy = reader.GetString(reader.GetOrdinal("VerifiedBy"));

      }
      return rate;
    }
    #endregion Fill Rating
  }
    */

	#endregion Old RatingsDAO
}